package com.dlwx.parttimetalent.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.StringUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NickEditActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_nick)
    EditText etNick;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            inputshoworhind();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_edit);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvMiddleTitle.setText("昵称");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        String nick = (String) SPUtils.get(this, Constant.NICK_NAME, "");
        etNick.setText(nick);
        mHandler.sendEmptyMessageDelayed(0, 500);
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_tips:
                tvTips.setVisibility(View.GONE);
                break;
            case R.id.tv_confirm:
                String nick = etNick.getText().toString();
                if (TextUtils.isEmpty(nick)) {
                    ToastUtils.toastForShort(this, "昵称不能为空");
                    return;
                }
                if (!StringUtil.isConSpeCharacters(nick)) {
                    ToastUtils.toastForShort(this, "昵称只能汉字、字母、数字和下划线及破折号");
                    return;
                }
                setUserInfor(nick);
                break;
        }
    }

    private void setUserInfor(final String name) {
        loadingDialog.show();
        OkgoUtil.post(Http.setuUserInfo, this)
                .params("infoName", "nickname")
                .params("values", name)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(NickEditActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            SPUtils.put(NickEditActivity.this, Constant.NICK_NAME, name);
                            NickEditActivity.this.finish();
                        }
                        ToastUtils.toastForShort(NickEditActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        inputshoworhind();
    }

    public void inputshoworhind() {

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
